package com.hj.security.ui;

import com.hj.security.R;
import com.hj.security.iservice.IService;
import com.hj.security.service.WatchDogService;
import com.hj.security.utils.MD5Encoder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LockActivity extends Activity {
	private ImageView iv_lock_app_icon;
	private TextView tv_lock_app_name;
	private EditText et_lock_pwd;
	private String password;
	private IService iService;
	private myServiceConnection serviceConnection;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lock);
		iv_lock_app_icon = (ImageView) findViewById(R.id.iv_lock_app_icon);
		tv_lock_app_name = (TextView) findViewById(R.id.tv_lock_app_name);
		et_lock_pwd = (EditText) findViewById(R.id.et_lock_pwd);
		
		//拿到真实的密码  
        //这里我们为了方便，就使用了手机防盗里面的那个密码，大家可以自己做成可以让用户设置的功能 
		password = getSharedPreferences("config", Context.MODE_PRIVATE).getString("password", "");
		
		try {
			String packageName = getIntent().getStringExtra("packageName");
			//通过包名拿到applicationInfo
			ApplicationInfo applicationInfo = getPackageManager().getPackageInfo(packageName, 0).applicationInfo;
			//应用图标
			Drawable app_icon = applicationInfo.loadIcon(getPackageManager());
			//应用名称
			String app_name = (String) applicationInfo.loadLabel(getPackageManager());
			
			iv_lock_app_icon.setImageDrawable(app_icon);
			tv_lock_app_name.setText(app_name);
			
			Intent intent = new Intent(this, WatchDogService.class);
			bindService(intent, serviceConnection, BIND_AUTO_CREATE);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}
	public void confirm(View v){
		String et_pwd = et_lock_pwd.getText().toString().trim();
		if(TextUtils.isEmpty(et_pwd)){
			Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
		}else if(password.equals(MD5Encoder.encode(et_pwd))){
			finish();
			String packageName =getIntent().getStringExtra("packageName");
			iService.stopApp(packageName);
		}else{
			Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
		}
	}
	//不让用户按后退键
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK == event.getKeyCode()){
			System.out.println("阻止返回键");
			return true;//阻止事件继续向下分发
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onDestroy() {
		if(serviceConnection!=null){
			unbindService(serviceConnection);
			serviceConnection = null;
		}
		super.onDestroy();
	}
	class myServiceConnection implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			System.out.println("LockActivity成功绑定WatchDogService");
			iService = (IService) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
	}	
}
